package com.framework.utils.pay.youfupay.method;

import com.alibaba.fastjson.JSONObject;
import com.framework.entity.sys.SysBank;
import com.framework.entity.sys.SysPayment;
import com.framework.exception.RRException;
import com.framework.utils.json.JsonUtils;
import com.framework.utils.pay.suixingfu.horfu.util.Base64Utils;
import com.framework.utils.pay.suixingfu.horfu.util.DESUtils;
import com.framework.utils.pay.suixingfu.horfu.util.RSAUtils;
import com.framework.utils.pay.youfupay.entity.PaymentTransModle;
import com.framework.utils.pay.youfupay.entity.RequestMessage;
import com.framework.utils.pay.youfupay.util.Constant;
import com.framework.utils.pay.youfupay.util.HttpClientHelper;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优付代付支付类
 *
 * @author wq
 * @date 2019/10/9 15:06
 * @description
 */
public class PayMethod {

    //static String url = "http://api.clooudnext.com:8280/pctrans-business/clientBusiness/common.do";
    static String url = "http://api.youfupingtai.com/clientBusiness/common.do";
    //static String url = "http://apitest.youfupingtai.com/pctrans-business/clientBusiness/common.do";
    public static String pay(SysPayment payment, SysBank bank, String identity, String secretKey, String money, String orderId, String batchId, String levyId) {
        RequestMessage rm = new RequestMessage();
        rm.setReqId("req" + System.currentTimeMillis());
        rm.setFunCode("6001");
        rm.setMerId(payment.getClientId());
        rm.setVersion("V1.0");
        String json = getJson(bank, identity, payment.getClientId(), money, payment.getPayName(), orderId, batchId, levyId);
        String detailData = null;
        RequestMessage requestMessage;
        try {
            byte[] bs = DESUtils.encrypt(json.getBytes(Constant.CHARSET), secretKey);

            String reqDataEncrypt = Base64Utils.encode(bs);
            rm.setReqData(reqDataEncrypt);
            rm.setSign(RSAUtils.sign(reqDataEncrypt, payment.getPrivateKey()));
            String reqStr = JsonUtils.toJson(rm);
            System.out.println("reqStr:" + reqStr);
            Map<String, Object> map = new HashMap<>();
            map.put("reqJson", reqStr);
            String resData = HttpClientHelper.httpClientPost(url, map, Constant.CHARSET);
            System.out.println("resData:" + resData);
            requestMessage = JsonUtils.fromJson(resData, RequestMessage.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("9000", e.getMessage());
        }
        if (!Constant.SUCCESS.equals(requestMessage.getResCode())) {
            if ("6000".equals(requestMessage.getResCode())) {
                throw new RRException("9000", "代付系统内部错误");
            } else {

                throw new RRException("9000", requestMessage.getResMsg());
            }
        }
        byte[] base64bs = Base64Utils.decode(requestMessage.getResData());
        try {
            byte[] debs = DESUtils.decrypt(base64bs, secretKey);

            detailData = new String(debs, Constant.CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("detailData:" + detailData);

        Map<String, Object> map = JSONObject.parseObject(detailData, Map.class);

        List<Map<String, String>> payResultList = (List<Map<String, String>>) map.get("payResultList");

        for (Map<String, String> stringStringMap : payResultList) {
            if (stringStringMap.get("merOrderId").equals(orderId)) {
                String resCode = stringStringMap.get("resCode");
                if (!Constant.SUCCESS.equals(resCode)) {
                    throw new RRException("9000", stringStringMap.get("resMsg"));
                }
            }
        }

        return detailData;
    }

    private static String getJson(SysBank bank, String identity, String merId, String money, String remark, String orderId, String batchId, String levyId) {
        PaymentTransModle pt = new PaymentTransModle();
        pt.setMerId(merId);
        pt.setTotalAmt(money);
        pt.setTotalCount("1");
        pt.setMerBatchId(batchId);
        List<PaymentTransModle.PayItems> list = new ArrayList<>();
        PaymentTransModle.PayItems pi1 = new PaymentTransModle.PayItems();
        pi1.setAmt(Long.valueOf(money));
        pi1.setBranchName("");
        pi1.setBranchNo("");
        pi1.setIdCard(identity);
        pi1.setMemo(remark);
        pi1.setMerOrderId(orderId);
        pi1.setMobile(bank.getPhone());
        pi1.setPayeeAcc(bank.getBankNo());
        pi1.setPayeeName(bank.getCardName());
        pi1.setPaymentType(0);
        pi1.setPayType(0);
        pi1.setLevyId(Long.valueOf(levyId));
        list.add(pi1);
        pt.setPayItems(list);
        String json = JsonUtils.toJson(pt);
        return json;
    }


}
